import 'dart:ffi';

import 'package:flutter/material.dart';

typedef SearchBarSearchCallback = void Function(String?);

class SearchBar extends StatefulWidget {
  final TextEditingController? controller;
  final Color? backgroundColor;
  final double height;
  final TextStyle? textStyle;
  final TextAlign textAlign;
  final Color? cursorColor;
  final String? hit;
  final TextStyle? hitStyle;
  final Widget? prefixIcon;
  final Widget? action;
  final double radius;
  final bool autofocus;
  final FocusNode? focusNode;
  final bool isEditing;
  final void Function(String)? onChanged;
  final void Function()? onEditingComplete;
  final void Function()? onTap;

  SearchBar({
    Key? key,
    this.controller,
    this.backgroundColor = Colors.white38,
    this.height = 40,
    this.textStyle,
    this.textAlign = TextAlign.left,
    this.cursorColor,
    this.hit,
    this.hitStyle = const TextStyle(color: Colors.grey),
    this.prefixIcon = const Icon(Icons.search),
    this.action,
    this.radius = 20,
    this.focusNode,
    this.autofocus = false,
    this.isEditing = true,
    this.onChanged,
    this.onEditingComplete,
    this.onTap,
  }) : super(key: key);

  createState() => SearchBarState();
}

class SearchBarState extends State<SearchBar> {
  @override
  Widget build(BuildContext context) {
    List<Widget> children = [_buildTextField()];
    if (widget.action != null) {
      children.add(SizedBox(width: 10));
      children.add(widget.action!);
    }
    return Row(
      children: children,
    );
  }

  Widget _buildTextField() {
    return Expanded(
      child: Container(
        height: widget.height,
        alignment: Alignment.centerLeft,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Expanded(
              child: GestureDetector(
                child: TextField(
                  enabled: widget.isEditing,
                  focusNode: widget.focusNode,
                  autofocus: widget.autofocus,
                  style: widget.textStyle,
                  textAlign: widget.textAlign,
                  controller: widget.controller,
                  cursorColor: widget.cursorColor,
                  onChanged: widget.onChanged,
                  onEditingComplete: widget.onEditingComplete,
                  onTap: widget.onTap,
                  decoration: InputDecoration(
                    contentPadding: EdgeInsets.only(top: 0, bottom: 0),
                    hintText: widget.hit,
                    hintStyle: widget.hitStyle,
                    prefixIcon: widget.prefixIcon,
                    focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                        color: Colors.transparent,
                      ),
                    ),
                    border: OutlineInputBorder(
                      borderSide: BorderSide(
                        color: Colors.transparent,
                      ),
                    ),
                    enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                        color: Colors.transparent,
                      ),
                    ),
                    disabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                        color: Colors.transparent,
                      ),
                    ),
                  ),
                ),
                onTap: widget.isEditing
                    ? null
                    : widget.onTap,
              ),
            ),
          ],
        ),
        decoration: BoxDecoration(
          color: widget.backgroundColor,
          borderRadius: BorderRadius.circular(widget.radius),
        ),
      ),
    );
  }
}
